package com.fh.shop.admin.controller.product;

import com.fh.shop.admin.biz.product.IProductService;
import com.fh.shop.admin.common.DataTableResult;
import com.fh.shop.admin.common.ServerResponse;
import com.fh.shop.admin.controller.FilePathController;
import com.fh.shop.admin.param.product.ProductAddParam;
import com.fh.shop.admin.param.product.ProductQueryParam;
import com.fh.shop.admin.param.product.ProductUpdateParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/product")
public class ProductController extends FilePathController {
    @Resource(name = "productService")
    private IProductService productService;

    @Resource
    private HttpServletRequest request;

    @GetMapping("/toAdd")
    public String toAdd(){
        return "product/add";
    }

    @GetMapping("/toList")
    public String toList(){
        return "product/list";
    }

    @GetMapping("/toUpdate")
    public String toUpdate(){
        return "/product/update";
    }

    @PostMapping("/add")
    @ResponseBody
    public ServerResponse add(ProductAddParam productAddParam){
        return productService.addProduct(productAddParam);
    }

    @GetMapping("/list")
    @ResponseBody
    public DataTableResult list(ProductQueryParam productQueryParam){
        return productService.findProductList(productQueryParam);
    }

    @PostMapping("/delete")
    @ResponseBody
    public ServerResponse delete(Long id){
        String root = getRealPath(request, "/");
        return productService.deleteProduct(id,root);
    }

    @PostMapping("/deleteBatch")
    @ResponseBody
    public ServerResponse deleteBatch(String ids){
        String root = getRealPath(request, "/");
        return productService.deleteBatch(ids,root);
    }

    @GetMapping("/find")
    @ResponseBody
    public ServerResponse findProduct(Long id){
        return productService.findProduct(id);
    }

    /**
     * 回显删除子图
     */
    @PostMapping("/deleteImage")
    @ResponseBody
    public ServerResponse deleteImage(Long key){
        String root = getRealPath(request, "/");
        return productService.deleteImage(key,root);
    }

    @PostMapping("/update")
    @ResponseBody
    public ServerResponse update(@RequestBody ProductUpdateParam productUpdateParam){
        String root = getRealPath(request, "/");
        return productService.updateProduct(productUpdateParam,root);
    }

    @PostMapping("/updateState")
    @ResponseBody
    public ServerResponse updateState(Long id,Integer state,String flag){
        return productService.updateState(id,state,flag);
    }

}
